package com.panshi.controller;

import com.panshi.entity.*;
import com.panshi.result.R;
import com.panshi.service.CommentClientService;
import com.panshi.service.UserClientService;
import com.panshi.service.ZxClientService;
import com.panshi.util.BaseUpload;
import com.panshi.util.QueryUtils;
import com.panshi.util.UserInfoUtils;
import com.panshi.util.ZxUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

@Controller
public class ConsultController {

    @Autowired
    private UserClientService userClientService;

    @Autowired
    private ZxClientService zxClientService;

    @Autowired
    private UserInfoUtils userInfoUtils;


    @Autowired
    private ZxUtils zxUtils;


    @Autowired
    private QueryUtils queryUtils;

    @Autowired
    private CommentClientService commentClientService;

    @Autowired
    private BaseUpload baseUpload;
    //用户发布文章
    @PostMapping("/zxAdd")
    @ResponseBody
    public R zxAdd(HttpServletRequest request) throws Exception {
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String essayimg = request.getParameter("essayimg");
        String classType = request.getParameter("classType");

        R r = new R();
        if(title==null || content ==null || classType == null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("请输入正确信息");
            return r;
        }
        String userId = request.getParameter("userId");
        if(userId==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("登录已失效，请重新登录！！！");
            return r;
        }


        SysEssayCheckT sysEssayCheckT = new SysEssayCheckT();
        sysEssayCheckT.setUserId(Integer.valueOf(userId));
        sysEssayCheckT.setEssayTitle(title);
        sysEssayCheckT.setEssayContent(content);
        sysEssayCheckT.setClassifyId(Integer.valueOf(classType));

        //咨询图片
        if(essayimg!=null){
            String s = baseUpload.put64myImage(essayimg);
            sysEssayCheckT.setImgPath(s);
        }

        System.out.println("添加咨询");
        System.out.println(sysEssayCheckT);

        Boolean aBoolean = zxClientService.zxAdd(sysEssayCheckT);
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(false);
            r.setMessage("发表成功 等待审核");
            return r;
        }
        return R.error();
    }


    @GetMapping("/zx/{id}")
    public String zxtype(@PathVariable("id") String id, Model model,HttpServletRequest request,HttpServletResponse response){
        model.addAttribute("zx","zx");
        if(id == null){
            return "/zx/404";
        }
        model.addAttribute("classId",id);
        //咨询分类列表
        List<ClassifyT> zxClassifyTList = zxClientService.call();
        model.addAttribute("zxClassifyTList",zxClassifyTList);

        //底部推荐咨询 6条
        List<AppEssayT> read_count = zxClientService.zxCPage(Integer.valueOf(id), 1, 6, "read_count");
        if(read_count!=null){
            read_count.forEach(essay->{
                essay.setClassifyT(zxClientService.bycid(essay.getClassifyId()));
            });
        }
        model.addAttribute("zxCEssayList",read_count);

        //咨询推荐加随机
        if(!id.equals("0")){
            model.addAttribute("className", zxClientService.bycid(Integer.valueOf(id)).getClassifyName());
        }
        List<AppEssayT> recommend  = zxUtils.zxcPage(Integer.valueOf(id), 1, 10, "10");
        model.addAttribute("recommend",recommend);
        //其他查询
        List<AppEssayT> rest  = zxUtils.zxPage(1,6,"15");
        model.addAttribute("rest",rest);
        userInfoUtils.veruser(request,model,response);
        return "consult/consult_main_type";
    }

    @PostMapping("/czxLoadInfo")
    @ResponseBody
    public R czxLoadInfo(HttpServletRequest request){
        String page = request.getParameter("page");
        String cid = request.getParameter("cid");
        String tabType = request.getParameter("tabType");

        if(tabType==null){
            tabType="10";
        }
        R r = new R();
        if(cid==null || page ==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("登录异常");
            return r;
        }

        List<AppEssayT> essaytList = zxUtils.zxcPage(Integer.valueOf(cid),Integer.valueOf(page),6,tabType);

        //底部推荐咨询 6条
        if(essaytList.isEmpty()){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("没有资源了");
            return r;
        }
        String data = zxUtils.zxSeek(essaytList);

        r.setCode(201);
        r.setSuccess(true);
        r.setMessage("请求成功");
        r.setData(data);

        return  r;
    }




    @RequestMapping("/consult_main")
    public String consultMain(HttpServletRequest request,Model model,HttpServletResponse response){
        model.addAttribute("zx","zx");
        List<ClassifyT> zxClassifyTList = zxClientService.call();
        //咨询分类列表
        model.addAttribute("zxClassifyTList",zxClassifyTList);

        //咨询
        List<AppEssayT> zxAppEssayTList = zxUtils.zxPage(1,10,"10");
        model.addAttribute("zxAppEssayTList",zxAppEssayTList);

        //咨询 大家都在看
        List<AppEssayT> zxreadEssays = zxUtils.zxPage(1,10,"15");
        Collections.shuffle(zxreadEssays);
        model.addAttribute("zxreadEssays",zxreadEssays);

        userInfoUtils.veruser(request,model,response);
        return "consult/consult_main";
    }

    @PostMapping("/zxSeek")
    @ResponseBody
    public R zxSeek(HttpServletRequest request){
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        String type = request.getParameter("type");
        R r = new R();

        if(page==null||limit==null || type==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("类型错误");
            return r;
        }


        //咨询
        List<AppEssayT> zxAppEssayTList = zxUtils.zxPage(Integer.valueOf(page),Integer.valueOf(limit),type);
        if(zxAppEssayTList.isEmpty()){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("没有数据了");
            return r;
        }
        String info = zxUtils.zxSeek(zxAppEssayTList);
        r.setCode(201);
        r.setSuccess(true);
        r.setMessage("查询成功");
        r.setData(info);

        return r;
    }




    @GetMapping("/consult_solo/{id}")
    public String consultSolo(HttpServletRequest request, Model model, @PathVariable int id, HttpServletResponse response){
        System.out.println(id);

        AppEssayT zxbyid = zxClientService.zxbyid(id);

        if(zxbyid==null){
            return "error/404";
        }

        List<AppCommentT> commentTS = commentClientService.commZxId(zxbyid.getEssayId(), 1, 3);
        if(commentTS==null){
            return "error/404";
        }
        commentTS.forEach(item->{
            List<AppCommentT> commById = commentClientService.getCommById(item.getId());
            if(commById!=null){

                commById.forEach(data->{
                    AppUserT byid = userClientService.byid(data.getUserId());
                    if(byid!=null){
                        data.setUserT(byid);
                    }
                });
                item.setCommList(commById);
            }

            AppUserT byid = userClientService.byid(item.getUserId());
            if(byid!=null){
                item.setUserT(byid);
            }


        });
        System.out.println("用户评论信息");
        System.out.println(commentTS);
        model.addAttribute("commentTS",commentTS);

        //类型
        model.addAttribute("classId",zxbyid.getClassifyId());

        zxbyid.setClassifyT(zxClientService.bycid(zxbyid.getClassifyId()));
        model.addAttribute("soloEssay",zxbyid);

        //用户信息
        userInfoUtils.veruser(request,model,response);
        List<ClassifyT> zxClassifyTList = zxClientService.call();
        //咨询分类列表
        model.addAttribute("zxClassifyTList",zxClassifyTList);


        //热读排行
        List<AppEssayT> appEssayTS1 = zxUtils.zxList(1, 8, "read_count");
        Collections.shuffle(appEssayTS1);
        model.addAttribute("readEssayTS",appEssayTS1);

        //底部推荐咨询 十条
        List<AppEssayT> read_count = zxClientService.zxCPage(zxbyid.getClassifyId(), 1, 6, "read_count");
        if(read_count!=null){
            read_count.forEach(essay->{
                essay.setClassifyT(zxClientService.bycid(essay.getClassifyId()));
            });
        }
        model.addAttribute("zxCEssayList",read_count);

        userInfoUtils.veruser(request,model,response);


        //增加咨询浏览
        zxClientService.essayReadAdd(zxbyid.getEssayId());


        return "consult/consult_particulars";
    }

    @GetMapping("/consult_solo/moreCommentlists")
    public ModelAndView moreCommentlists(HttpServletRequest request, Model model, HttpServletResponse response){
        String essayId = request.getParameter("essayId");
        System.out.println(essayId);

        String page = request.getParameter("page");
        if(essayId==null || page==null){
            return new ModelAndView("consult/comment_list");
        }
        List<AppCommentT> commList = zxUtils.commList(Integer.valueOf(essayId),Integer.valueOf(page));
        model.addAttribute("commList",commList);
        userInfoUtils.veruser(request,model,response);
        return new ModelAndView("consult/comment_list");
    }


    @PostMapping("/queryLimit")
    @ResponseBody
    public R queryLimit(HttpServletRequest request,Model model){

        R r = new R();
        String limit =request.getParameter("limit");

        if(limit==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("没有数据了");
            return r;
        }
        String type = request.getParameter("type");
        String page = request.getParameter("page");


        List list = queryUtils.queryLimit(type,Integer.valueOf(page), Integer.valueOf(limit));

        if(list.isEmpty()){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("没有数据了");
            return r;
        }

        System.out.println(list.size()+"  list   ---------------->");
        String data = queryUtils.dataInfo(list);


        r.setCode(201);
        r.setSuccess(true);
        r.setMessage("查询成功");
        r.setData(data);
        return r;
    }
}
